import { createRouter, createWebHashHistory } from 'vue-router'
import { Toast } from 'vant'

import 'vant/es/toast/style'
const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/home/homeView.vue')
  },

  {
    path: '/search',
    name: 'search',
    component: () => import('@/views/home/homeSearch.vue')
  },
  //美食模块
  {
    path: '/foodlist',
    name: 'foodlist',
    component: () => import('@/views/foodlist/foodCenter.vue')
  },
  {
    path: '/fddetail/:id',
    name: 'fddetail',
    component: () => import('@/views/foodlist/fdDetail.vue')
  },
  //美食购物车
  {
    path: '/foodcar',
    name: 'foodcar',
    component: () => import('@/views/foodlist/foodCar.vue')
  },
  {
    path: '/golist',
    name: 'golist',
    component: () => import('@/views/foodlist/goList.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/home/loginView.vue')
  },
  {
    path: '/movie',
    name: 'movie',
    component: () => import('@/views/movie/MovieIndexView.vue'),
    children: [
      {
        //bbx
        path: '/movie',
        redirect: { name: 'moviehot' }
      },

      {
        //bbx
        path: 'moviehot',
        name: 'moviehot',
        component: () => import('@/views/movie/MovieHotView.vue'),
        children: [
          {
            path: 'movielist',
            name: 'movielist',
            component: () => import('@/views/movie/MovieListView.vue')
          }
        ]
      },
      {
        path: '/moviedetail/:id',
        name: 'moviedetail',
        component: () => import('@/views/movie/MovieDetailView.vue')
      },
      {
        //fsz
        path: 'cinema',
        name: 'cinema',
        component: () => import('@/views/movie/CinemaView.vue'),
        children: []
      },
      //fsz
      {
        path: '/cinemainfo/:id',
        name: 'cinemainfo',
        component: () => import('@/views/movie/CinemaInfoView.vue')
      },
      //fsz
      {
        path: '/cinemafind/:id',
        name: 'cinemafind',
        component: () => import('@/views/movie/CinemafindView.vue')
      },

      {
        //zjj
        path: 'content',
        name: 'content',
        component: () => import('@/views/movie/ContentView.vue')
      },
      {
        //zjj
        path: 'orderlist',
        name: 'orderlist',
        component: () => import('@/views/movie/OrderListView.vue')
      },
      {
        //zjj
        path: 'shoplist',
        name: 'shoplist',
        component: () => import('@/views/movie/ShopView.vue')
      },
      {
        //zjj
        path: 'movielogin',
        name: 'movie',
        component: () => import('@/views/movie/MyCenterLogin.vue')
      }
    ]
  },
  {
    // 外卖
    path: '/food',
    name: 'food',
    redirect: '/shop',
    component: () => import('@/views/FoodIndex.vue'),
    children: [
      // 外卖商品页面
      {
        path: '/shop',
        name: 'shop',
        component: () => import('@/views/food/Shop.vue')
      },
      {
        path: '/shopCategory',
        name: 'shopCategory',
        component: () => import('@/views/food/ShopChild/ShopCategory.vue')
      },
      {
        path: '/shopDetail',
        name: 'shopDetail',
        component: () => import('@/views/food/ShopChild/ShopDetail.vue')
      },
      {
        //lmz
        path: '/order',
        name: 'order',
        component: () => import('@/views/food/Order.vue')
      },
      {
        path: '/orderdetail',
        name: 'orderdetail',
        component: () => import('@/views/food/Orderdetail.vue')
      },
      {
        path: '/orderdispatching',
        name: 'orderdispatching',
        component: () => import('@/views/food/orderdispatching.vue')
      },
      {
        path: '/profile',
        name: 'profile',
        component: () => import('@/views/food/Profile.vue')
      },

      {
        // 神券专区可选
        path: '/prefecture',
        name: 'prefecture',
        component: () => import('@/views/food/Prefecture.vue')
      }
    ]
  },
  {
    path: '/redpack',
    name: 'redpack',
    component: () => import('@/views/food/Mylist/Redpack.vue')
  },
  {
    path: '/shippingaddress',
    name: 'shippingaddress',
    component: () => import('@/views/food/Mylist/Shippingaddress.vue')
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: () => import('@/views/home/homeDetail.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
// 判断本地储存是否有数据
const isLogin = () => {
  return !!localStorage.getItem('adminname')
}
// 路由前置守卫
router.beforeEach((to) => {
  if (to.name === 'login') {
    if (isLogin()) {
      Toast.fail('请勿重复登录')
      return { name: 'home' }
    }
  } else {
    if (!isLogin()) {
      return { name: 'login' }
    }
  }
})
export default router
